﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.LookAndFeel;
using DevExpress.XtraSplashScreen;
using System.Threading;
using DevExpress.XtraBars.Ribbon;
using p = Microsoft.Office.Interop.PowerPoint;
using System.IO;
using SMART_VIET_V01.Lib;
using System.Collections;

namespace SMART_VIET_V01
{
    public partial class frm_main : DevExpress.XtraEditors.XtraForm
    {
        BackgroundWorker backGW_loadform;
        SQL_Functions funs = new SQL_Functions();

        public frm_main()
        {
            SplashScreenManager.ShowForm(typeof(frm_splash));
            InitializeComponent();
            //UserLookAndFeel.Default.SetSkinStyle("Office 2010 Blue");
            this.Hide();
            //loadImages();
            loadingView();
            loadKhoilop();
            try
            {
                loadMonhoc(cb_khoilop.SelectedValue.ToString());
                loadChuyende(cb_monhoc.SelectedValue.ToString());
                loadNhombaigiang(cb_monhoc.SelectedValue.ToString());
                loadBaigiang(cb_nh_baigiang.SelectedValue.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            SplashScreenManager.CloseForm();
            this.Show();
        }
        void loadingView()
        {
            try
            {
                powerpoint_frame.CreateNew("PowerPoint.Show");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Sự cố trong quá trình mở file !");
                Application.Exit();
            }

        }
        Dictionary<string, Image> listImg = new Dictionary<string, Image>();
        public Image convert(byte[] _byte)
        {
            Image newImage;
            using (MemoryStream stream = new MemoryStream(_byte))
            {
                newImage = System.Drawing.Image.FromStream(stream);
            }
            return newImage;
        }
        void loadImages()
        {
            ga_hinhanh.Gallery.Groups.Clear();
            string sql = "SELECT * FROM  [tblMedia] where chuyende_id = " + chuyende_id;
            DataTable dt = funs.returnDB(sql);
            ga_hinhanh.Gallery.ImageSize = new System.Drawing.Size(45, 45);
            ga_hinhanh.Gallery.ItemImageLayout = DevExpress.Utils.Drawing.ImageLayoutMode.ZoomInside;
            ga_hinhanh.Gallery.HoverImageSize = new System.Drawing.Size(200, 200);
            //ga_hinhanh.Gallery.ImageSize.Width = 45;
            listImg.Clear();

            if (dt != null)
            {
                GalleryItemGroup gr_h_01 = new GalleryItemGroup();
                gr_h_01.Caption = "Thư viện hình";
                ga_hinhanh.Gallery.Groups.Add(gr_h_01);
                foreach (DataRow row in dt.Rows)
                {
                    if (row["mediatype_id"].ToString().Equals("1"))
                    {
                        GalleryItem item = new GalleryItem();
                        item.Tag = row["stt"].ToString();
                        Image img = convert((byte[])row["media"]);
                        listImg.Add(row["stt"].ToString(), img);
                        item.Image = img;
                        item.HoverImage = img;
                        gr_h_01.Items.Add(item);
                    }

                }
            }



            //ga_Video.Gallery.Images = phim_amthanh;
            //ga_Voice.Gallery.Images = phim_amthanh;
            ////Galery Hinh anh
            //ga_hinhanh.Gallery.Images = hinhanh;
            //ga_hinhanh.Gallery.HoverImages = hinhanh_hover;

            //GalleryItemGroup gr_h_01 = new GalleryItemGroup();
            //gr_h_01.Caption = "Tập đọc";
            //GalleryItemGroup gr_h_02 = new GalleryItemGroup();
            //gr_h_02.Caption = "Ngữ pháp";
            //ga_hinhanh.Gallery.Groups.Add(gr_h_01);
            //ga_hinhanh.Gallery.Groups.Add(gr_h_02);

            //for (int i = 0; i < 9; i++)
            //{
            //    GalleryItem item = new GalleryItem();
            //    item.ImageIndex = i;
            //    item.HoverImageIndex = i;
            //    item.Caption = "Hình " + i;
            //    gr_h_01.Items.Add(item);
            //}
            //for (int i = 0; i < 9; i++)
            //{
            //    GalleryItem item = new GalleryItem();
            //    item.ImageIndex = i;
            //    item.HoverImageIndex = i;
            //    item.Caption = "Hình " + (i + 9);
            //    gr_h_01.Items.Add(item);
            //}
            //for (int i = 0; i < 9; i++)
            //{
            //    GalleryItem item = new GalleryItem();
            //    item.ImageIndex = i;
            //    item.HoverImageIndex = i;
            //    item.Caption = "Hình " + i;
            //    gr_h_02.Items.Add(item);
            //}
            //for (int i = 0; i < 9; i++)
            //{
            //    GalleryItem item = new GalleryItem();
            //    item.ImageIndex = i;
            //    item.HoverImageIndex = i;
            //    item.Caption = "Hình " + (i + 9);
            //    gr_h_02.Items.Add(item);
            //}



            ////Galery Video
            //ga_Video.Gallery.ShowItemText = true;
            //GalleryItemGroup gr_01 = new GalleryItemGroup();
            //gr_01.Caption = "Tập đọc";
            //GalleryItemGroup gr_02 = new GalleryItemGroup();
            //gr_02.Caption = "Ngữ pháp";

            //ga_Video.Gallery.Groups.Add(gr_01);
            //ga_Video.Gallery.Groups.Add(gr_02);

            //for (int i = 0; i < 20; i++)
            //{
            //    GalleryItem item = new GalleryItem();
            //    item.ImageIndex = 0;
            //    item.Caption = "Video " + i;
            //    gr_01.Items.Add(item);
            //}
            //for (int i = 0; i < 20; i++)
            //{
            //    GalleryItem item = new GalleryItem();
            //    item.ImageIndex = 0;
            //    item.Caption = "Video " + i;
            //    gr_02.Items.Add(item);
            //}

            ////Galery Voice
            //ga_Voice.Gallery.ShowItemText = true;
            //GalleryItemGroup gr_v_01 = new GalleryItemGroup();
            //gr_v_01.Caption = "Tập đọc";
            //GalleryItemGroup gr_v_02 = new GalleryItemGroup();
            //gr_v_02.Caption = "Ngữ pháp";

            //ga_Voice.Gallery.Groups.Add(gr_v_01);
            //ga_Voice.Gallery.Groups.Add(gr_v_02);

            //for (int i = 0; i < 20; i++)
            //{
            //    GalleryItem item = new GalleryItem();
            //    item.ImageIndex = 1;
            //    item.Caption = "Audio " + i;
            //    gr_v_01.Items.Add(item);
            //}
            //for (int i = 0; i < 20; i++)
            //{
            //    GalleryItem item = new GalleryItem();
            //    item.ImageIndex = 1;
            //    item.Caption = "Audio " + i;
            //    gr_v_02.Items.Add(item);
            //}
        }

        private void MForm_Load(object sender, EventArgs e)
        {

        }
        private void axEDOffice1_NewDocument(object sender, EventArgs e)
        {

        }

        private void nav_vehinh_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frm_vehinh frm = new frm_vehinh();
            frm.ShowDialog();
        }

        private void nav_vedothi_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frm_vedothi frm = new frm_vedothi(this);
            frm.ShowDialog();
        }

        private void nav_tracnghiem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frm_tracnghiem frm = new frm_tracnghiem();
            frm.ShowDialog();
        }

        private void nav_quayphim_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frm_quayphim frm = new frm_quayphim();
            frm.ShowDialog();
        }

        private void nav_thuam_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frm_thuam frm = new frm_thuam();
            frm.ShowDialog();
        }

        private void ga_hinhanh_GalleryPopupClose(object sender, InplaceGalleryEventArgs e)
        {
            //ga_hinhanh.Gallery.ShowGroupCaption = false;
            //ga_hinhanh.Gallery.RefreshGallery();
        }
        private void ga_hinhanh_GalleryInitDropDownGallery(object sender, InplaceGalleryEventArgs e)
        {
            ga_hinhanh.Gallery.ShowGroupCaption = true;
            //ga_hinhanh.Gallery.RefreshGallery();
        }
        bool flag = true;
        private void xtraTabControl1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (flag)
            {
                xtraTabControl1.Height = 30;
                flag = false;
            }

            else
            {
                xtraTabControl1.Height = 155;
                flag = true;
            }
        }

        private void ga_hinhanh_Gallery_ItemDoubleClick(object sender, GalleryItemClickEventArgs e)
        {
            Image img;
            listImg.TryGetValue(e.Item.Tag.ToString(), out img);
            string name = e.Item.Caption;
            themHinhBaiGiang(img, name);
        }

        public void themHinhBaiGiang(Image img, string name)
        {
            p.Presentation doc = (p.Presentation)powerpoint_frame.ActiveDocument;
            int id = doc.Application.ActiveWindow.Selection.SlideRange.SlideNumber;
            p.Slides slides = doc.Slides;
            p.Slide slide = slides[id];
            p.Shapes shapes = slide.Shapes;
            string uri = String.Format(@"{0}\temp\" + name + ".png", System.IO.Path.GetDirectoryName(
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase));
            string fileName = new Uri(uri).LocalPath;
            img.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
            slide.Shapes.AddPicture(fileName, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 0, img.Width, img.Height);
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
        }

        private void cb_khoilop_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                khoilop_id = cb_khoilop.SelectedValue.ToString();
                cb_monhoc.DataSource = null;
                cb_chuyende.DataSource = null;
                cb_nh_baigiang.DataSource = null;
                list_baigiang.Clear();
                loadMonhoc(khoilop_id);
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }

        }

        private void cb_monhoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                monhoc_id = cb_monhoc.SelectedValue.ToString();
                loadChuyende(monhoc_id);
                loadNhombaigiang(monhoc_id);
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }

        }

        private void cb_chuyende_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                chuyende_id = cb_chuyende.SelectedValue.ToString();
                loadImages();
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }

        }

        private void cb_nh_baigiang_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                nhombaigiang_id = cb_nh_baigiang.SelectedValue.ToString();
                loadBaigiang(nhombaigiang_id);
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }

        }
        private void bt_themtuongtac_Click(object sender, EventArgs e)
        {
            frm_themtuongtac frm = new frm_themtuongtac(this, monhoc_id, tuongtac);
            frm.ShowDialog();
        }

        private void bt_xoa_het_tuongtac_Click(object sender, EventArgs e)
        {
            this.tuongtac.Rows.Clear();
            list_tuongtac.Items.Clear();
        }

        private void xoa_menu_Click(object sender, EventArgs e)
        {

        }

        #region function
        //function
        string chuyende_id;
        string khoilop_id;
        string monhoc_id;
        string nhombaigiang_id;

        void loadKhoilop()
        {
            cb_khoilop.DataSource = null;
            string sql = "Select * from tblKhoiLop";
            DataTable dt = funs.returnDB(sql);
            cb_khoilop.DataSource = dt;
            cb_khoilop.DisplayMember = "ten_khoilop";
            cb_khoilop.ValueMember = "stt";
        }
        void loadMonhoc(string khoilop_id)
        {
            cb_monhoc.DataSource = null;
            string sql = "Select * from tblMonHoc where khoilop_id = " + khoilop_id;
            DataTable dt = funs.returnDB(sql);
            cb_monhoc.DataSource = dt;
            cb_monhoc.DisplayMember = "ten_monhoc";
            cb_monhoc.ValueMember = "stt";
        }
        void loadChuyende(string monhoc_id)
        {
            cb_chuyende.DataSource = null;
            string sql = "Select * from tblMediaLib where monhoc_id = " + monhoc_id;
            DataTable dt = funs.returnDB(sql);
            cb_chuyende.DataSource = dt;
            cb_chuyende.DisplayMember = "ten_chuyende";
            cb_chuyende.ValueMember = "stt";
        }
        void loadNhombaigiang(string monhoc_id)
        {
            cb_nh_baigiang.DataSource = null;
            string sql = "Select * from tbl_NhomBaiGiang where monhoc_id = " + monhoc_id;
            DataTable dt = funs.returnDB(sql);
            cb_nh_baigiang.DataSource = dt;
            cb_nh_baigiang.DisplayMember = "ten_nhombaigiang";
            cb_nh_baigiang.ValueMember = "stt";
        }
        void loadBaigiang(string nhombaigiang_id)
        {
            list_baigiang.Clear();
            string sql = "Select * from tblBaiGiang where nhombaigiang_id = " + nhombaigiang_id;
            DataTable dt = funs.returnDB(sql);
            foreach (DataRow item in dt.Rows)
            {
                ListViewItem list_item = new ListViewItem();
                list_item.ImageIndex = 0;
                list_item.Text = item["ten_baigiang"].ToString();
                ListViewItem.ListViewSubItem subitem = new ListViewItem.ListViewSubItem();
                subitem.Text = item["stt"].ToString();
                ListViewItem.ListViewSubItem dir = new ListViewItem.ListViewSubItem();
                dir.Text = item["noidung"].ToString();
                list_item.SubItems.Add(subitem);
                list_item.SubItems.Add(dir);
                list_baigiang.Items.Add(list_item);
            }
        }
        DataTable tuongtac = new DataTable();

        public void loadTuongtac(DataTable _tuongtac)
        {
            list_tuongtac.Items.Clear();
            this.tuongtac = _tuongtac;
            foreach (DataRow item in _tuongtac.Rows)
            {
                ListViewItem tuongtac_item = new ListViewItem();
                tuongtac_item.Text = item["ten_tuongtac"].ToString();
                ListViewItem.ListViewSubItem tuongtac_id = new ListViewItem.ListViewSubItem();
                tuongtac_id.Text = item["stt"].ToString();
                tuongtac_item.SubItems.Add(tuongtac_id);
                list_tuongtac.Items.Add(tuongtac_item);
            }
            list_tuongtac.Refresh();

        }
        public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            return ms.ToArray();
        }
        public Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }

        #endregion

        private void bar_bt_tailieumoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            powerpoint_frame.CreateNew("PowerPoint.Show");
        }

        private void bar_bt_Mofile_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK) // Test result.
            {
                powerpoint_frame.Open(openFileDialog1.FileName);
            }
            Console.WriteLine(result); // <-- For debugging use.
        }

        private void bar_bt_media_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frm_quanlyMedias frm = new frm_quanlyMedias();
            frm.ShowDialog();
        }

        private void bar_bt_tuongtac_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frm_quanlyTuongTac frm = new frm_quanlyTuongTac();
            frm.ShowDialog();
        }



    }
}